'use strict';

angular.module('myApp.teachers_detail', [])
.controller('teachers_detailCtrl',['$scope','$http','$routeParams','breadcrumbData',function($scope,$http,$routeParams,$breadcrumbData){
    if($routeParams.id){
        $http.get('v1/teacher/'+ $routeParams.id).success(function(data){
            data.result[0].teach_age *= 1;
            data.result[0].base_salary *= 1;
            data.result[0].remain_salary *= 1;
            data.result[0].month_house_loan *= 1;
            $scope.teacher = data.result[0];
        });    
        $scope.action = "编辑"; 
    }else{

        $scope.teacher = {};
        $scope.action = "新建";
    }
    $breadcrumbData[3] = {name:$scope.action};

    //清空 提交
    $scope.submit = function(){ 

        if($scope.teacher.id){
            $http.put('v1/teacher/'+ $routeParams.id,$scope.teacher).success(function(data){

                if( !data.error ){
                    alert("修改成功","success");
                    window.location.href = '#/teachers';
                }else{
                    alert(data.message);
                }
            });
        }else{

             $http.post('v1/teacher',$scope.teacher).success(function(data){
                console.log(data);
                if( !data.error ){
                    alert("添加成功","success");
                    //window.location.href = '#/teachers';
                }else{
                    alert(data.message);
                }
            });           
        }
    };

    // 日历
    $('.form-date').datetimepicker(
    {
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: 'yyyy-mm-dd'
    });
}]);